﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sbartholomeusz.Lib;
using System.Net;

namespace Sbartholomeusz.WinGet.Console
{
    class MainProgram
    {
        /// <summary>
        /// Main program
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Uri fileDownloadURL = null;
            string outputDirectory = String.Empty;
            string outputFilename = String.Empty;

            PrintOutput("HttpUtils - " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")); //TODO: move title to resource file
            PrintOutput("");
            PrintOutput("*******************************");

            #if DEBUG
                //TODO: For debug only
                fileDownloadURL = new Uri(@"https://www.google.com.au/images/srpr/logo11w.png");
                outputDirectory = @"C:\temp\";
            #endif

            // Process arguments
            if (args.Count() >= 2)
            {
                fileDownloadURL = new Uri(args[0]);
                outputDirectory = args[1];
            }

            if (args.Count() >= 3)
            {
                outputFilename = args[2];
            }


            // Prompt for params if not specified
            while (fileDownloadURL == null)
            {
                PrintOutput("Please specify the file download URL:");
                String input = System.Console.In.ReadLine();

                if (!string.IsNullOrEmpty(input))
                {
                    try
                    {
                        fileDownloadURL = new Uri(input);
                    }

                    catch (UriFormatException e)
                    {
                        PrintOutput("");
                        PrintOutput("ERROR: Invalid URL specified");
                    }
                }
            }

            while (String.IsNullOrEmpty(outputDirectory))
            {
                PrintOutput("Please specify the file output directory:");
                outputDirectory = System.Console.In.ReadLine();
            }

            while (string.IsNullOrEmpty(outputFilename))
            {
                outputFilename = HttpUtils.GetFileName(fileDownloadURL.ToString());

                while (string.IsNullOrEmpty(outputFilename))
                {
                    PrintOutput("Please specify the output filename:");
                    outputFilename = System.Console.In.ReadLine();
                }
            }
            //


            PrintOutput("");
            PrintOutput("Parameters:");
            PrintOutput("  File Download URL: " + fileDownloadURL);
            PrintOutput("  Output File Path: " + outputDirectory);
            PrintOutput("  Output Filename: " + outputFilename);
            //

            PrintOutput("*******************************");
            PrintOutput("");

            PrintOutput("Downloading file from " + fileDownloadURL + " to " + outputDirectory + @"\" + outputFilename + " ...");
            HttpOperationStatus hos = HttpUtils.GetFile(fileDownloadURL, @"C:\temp\" + outputFilename);

            if (hos.StatusCode == HttpOperationStatus.Statuses.STATUS_SUCCESS)
            {
                PrintOutput("Download successful");
                PrintOutput("Duration: " + hos.RequestDuration + " seconds");
            }
            else if (hos.StatusCode == HttpOperationStatus.Statuses.STATUS_ERROR)
            {
                PrintOutput(hos.StatusMessage);
            }

            PrintOutput("");
            PrintOutput("End Date/Time: " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
            PrintOutput("");
            PrintOutput("Press ENTER to close window");

            System.Console.In.ReadLine();
            System.Environment.Exit(0);
        }

        /// <summary>
        /// Print output
        /// </summary>
        /// <param name="output"></param>
        private static void PrintOutput (string output)
        {
            System.Console.WriteLine(output);
        }
    }
}
